home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / delicious_bookmarks-2.0.64-fx.xpi / components / nsYDebugService.js < prev    next >
Text File  |  2008-06-19  |  7KB  |  206 lines

  1. const nsIYDebugService = Components.interfaces.nsIYDebugService;
  2.  
  3. const CLASS_ID = Components.ID("{559013d0-6099-11db-b0de-0800200c9a66}");
  4. const CLASS_NAME = "Service to print log and debug messages";
  5. const CONTRACT_ID = "@mozilla.org/ybookmarks-debug-service;1";
  6.  
  7. /**
  8.  * Class definition
  9.  */
  10. var YDebugService = {
  11.     _debugEnabled: null,
  12.     _logEnabled: null, 
  13.     _logFileName: "ybookmarks@yahoo.log",
  14.     _maxLogFileSize: 1 * 1048 * 1048,
  15.     _maxLogLines: 10000,
  16.     _logFile: null,
  17.     _logStream: null,
  18.     _nLogLinesPrinted: 0,
  19.     _lastLogTimeStamp: 0,
  20.     _lastLogTimeString: "",
  21.  
  22.     _getPrefs: function() {
  23.         this._logEnabled = this._debugEnabled = false;
  24.         try {
  25.         var prefs = 
  26.                 Components.classes[ "@mozilla.org/preferences-service;1" ].getService( 
  27.                     Components.interfaces.nsIPrefBranch );
  28.         this._logEnabled = prefs.getBoolPref( "extensions.ybookmarks@yahoo.log" );
  29.         this._debugEnabled = prefs.getBoolPref( "extensions.ybookmarks@yahoo.debug" );
  30.         }
  31.         catch( e ) { }
  32.     },
  33.  
  34.     init: function() {
  35.         this._getPrefs();
  36.         this._createLogFile();
  37.         this._createLogStream();
  38.     },
  39.     
  40.     on: function( refresh ) {
  41.         if( refresh ) {
  42.             this._getPrefs();
  43.         }
  44.         return this._debugEnabled;
  45.     },
  46.  
  47.     _createLogFile: function() {
  48.         var dirService = 
  49.             ( Components.classes[ "@mozilla.org/file/directory_service;1" ] ).getService( 
  50.                 Components.interfaces.nsIProperties );
  51.         this._logFile = 
  52.             dirService.get( "ProfD", Components.interfaces.nsILocalFile );
  53.         this._logFile.append( this._logFileName );
  54.         if( this._logFile.exists() && ( this._logFile.fileSize > this._maxLogFileSize ) ) {
  55.             this._logFile.remove( false );
  56.         }
  57.         if( !this._logFile.exists() ) {
  58.             this._logFile.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 00644 );
  59.         }
  60.     },
  61.  
  62.     _createLogStream: function() {
  63.         this._logStream = 
  64.             ( Components.classes[ "@mozilla.org/network/file-output-stream;1" ] ).createInstance( 
  65.                 Components.interfaces.nsIFileOutputStream );
  66.         // opening in append and write-only mode
  67.         this._logStream.init( this._logFile, 0x10 | 0x02, 00644, 0 );
  68.         this._nLogLinesPrinted = 0;
  69.         this._lastLogTimeStamp = 0;
  70.         this._lastLogTimeString = "";
  71.         this._printVersion();
  72.     },
  73.  
  74.     _printVersion: function() {
  75.     var bundleService = 
  76.             Components.classes[ "@mozilla.org/intl/stringbundle;1" ].getService( 
  77.                 Components.interfaces.nsIStringBundleService );
  78.         var bundle = 
  79.             bundleService.createBundle( "chrome://ybookmarks/locale/ybookmarks.properties" );
  80.     var versionNum = bundle.GetStringFromName( "extensions.ybookmarks.versionNum" );
  81.         this.printLog( "*********** LOG CREATED. EXT VER: " + versionNum + " *************" );
  82.         
  83.         var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
  84.                         .getService(Components.interfaces.nsIXULAppInfo);        
  85.         var assClass =
  86.             Components.classes["@mozilla.org/appshell/appShellService;1"];
  87.         var ass = assClass.getService(Components.interfaces.nsIAppShellService);
  88.         var hiddenWin = ass.hiddenDOMWindow;
  89.         var platform = hiddenWin.navigator.platform;  
  90.  
  91.         this.printLog( "*********** OS: " + platform + "  FX VER: " + appInfo.version + " *************" );
  92.     },
  93.  
  94.     _getTimeField: function() {
  95.         var now = new Date();
  96.         var timeField;
  97.         // create new timestamp only if a minute has passed
  98.         if( ( now.getTime() - this._lastLogTimeStamp ) > 60 ) {
  99.             timeField = "[" + 
  100.                 now.getFullYear() + "/" + ( now.getMonth() + 1 ) + "/" + now.getDate() + " " +
  101.                 now.getHours() + ":" + now.getMinutes()
  102.                 + "] ";
  103.             this._lastLogTimeString = timeField;
  104.             this._lastLogTimeStamp = now.getTime();
  105.         }
  106.         else {
  107.             timeField = this._lastLogTimeString;
  108.         }
  109.         return timeField;
  110.     },
  111.  
  112.     printDebug: function( message ) {
  113.         if( this._debugEnabled ) {
  114.             this.printLog( message );
  115.         }
  116.     },
  117.  
  118.     printLog: function( message ) {
  119.         // dump to file
  120.         try {
  121.             if( this._nLogLinesPrinted > this._maxLogLines ) {
  122.                 this._logStream.close();
  123.                 this._logFile.remove( false );
  124.                 this._createLogFile();
  125.                 this._createLogStream();
  126.             }
  127.             var logMessage = this._getTimeField();
  128.             logMessage += message;
  129.             logMessage += "\n";
  130.             this._logStream.write( logMessage, logMessage.length );
  131.             ++this._nLogLinesPrinted;
  132.         }
  133.         catch( e ) {
  134.             if( this._logEnabled ) {
  135.                 dump( "exception while trying to write to log file: " + e + "\n" );
  136.             }
  137.         }
  138.         
  139.         // now dump to console
  140.         if( this._logEnabled ) {
  141.             dump( message + '\n' );
  142.         }
  143.     },
  144.  
  145.     QueryInterface: function(aIID) {
  146.         if( !aIID.equals( nsIYDebugService ) && !aIID.equals( nsISupports ) ) {
  147.             throw Components.results.NS_ERROR_NO_INTERFACE;
  148.         }
  149.         return this;
  150.     }
  151.  
  152. };
  153.  
  154.  
  155. /* 
  156.  * Class factory
  157.  */
  158. var YDebugServiceFactory = {
  159.    _singletonObj: null,
  160.     createInstance: function( aOuter, aIID ) {
  161.         if ( aOuter != null ) {
  162.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  163.         }
  164.         if ( !this._singletonObj ) {
  165.             YDebugService.init();
  166.             this._singletonObj = YDebugService;
  167.         }
  168.         return this._singletonObj.QueryInterface( aIID );
  169.     }
  170. };
  171.  
  172.  
  173. /*
  174.  * Module definition
  175.  */
  176. var YDebugServiceModule = {
  177.     registerSelf: function( aCompMgr, aFileSpec, aLocation, aType ) {
  178.         aCompMgr = aCompMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
  179.         aCompMgr.registerFactoryLocation( CLASS_ID, CLASS_NAME, CONTRACT_ID, 
  180.                                           aFileSpec, aLocation, aType);
  181.     },
  182.  
  183.     unregisterSelf: function( aCompMgr, aLocation, aType ) {
  184.         aCompMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
  185.         aCompMgr.unregisterFactoryLocation( CLASS_ID, aLocation );
  186.     },
  187.  
  188.     getClassObject: function( aCompMgr, aCID, aIID ) {
  189.         if( !aIID.equals( Components.interfaces.nsIFactory ) ) {
  190.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  191.         }
  192.         if( aCID.equals( CLASS_ID ) ) {
  193.             return YDebugServiceFactory;
  194.         }
  195.         throw Components.results.NS_ERROR_NO_INTERFACE;
  196.     },
  197.  
  198.     canUnload: function( aCompMgr ) {
  199.         return true;
  200.     }
  201. };
  202.  
  203. function NSGetModule( aCompMgr, aFileSpec ) {
  204.     return YDebugServiceModule;
  205. }
  206.